Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
'Imports DotNetNuke.Installer
Imports ICSharpCode.SharpZipLib.Zip
Imports System.IO
Imports DotNetNuke
Imports System.Net

Namespace NVPortal.Modules.NVProduct

    Public MustInherit Class Edit
        Inherits Entities.Modules.PortalModuleBase
        Dim sanpham As String

#Region "Event Handlers"
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not IsPostBack Then
                Try
                    BindddlCategories()
                    '<Edit news>
                    If Request.Item("sanpham") <> "" Then
                        'Me.tdTinlienquan.Visible = True
                        Dim sanpham As String
                        sanpham = Request.Item("sanpham")
                        ' Hien thi tin nay
                        Dim ctlNews As New ProductController
                        Dim objNews As ProductInfo
                        objNews = ctlNews.Product_GetByID(Request.Item("sanpham"), PortalId)
                        Me.txtTitle.Text = objNews.Tensanpham
                        Me.txtTitlePhu.Text = objNews.Tenphu
                        Me.ddlCategory.SelectedValue = objNews.CategoryId
                        Me.txtMasp.Text = objNews.Masapham
                        Me.txtGia.Text = objNews.Gia
                        Me.txtGiaKM.Text = objNews.GiaKM
                        Me.chkSPKM.Checked = objNews.SPKM
                        Me.txtgiatriKM.text = objNews.GiatriKM
                        Me.txtChatlieu.Text = objNews.Chatlieu
                        Me.txtXuatxu.Text = objNews.Xuatxu
                        If Not objNews.Avatar Is Nothing Then
                            If objNews.Avatar.ToString.Contains("http://") Then
                                Me.imgNews.Visible = True
                                Me.imgNews.ImageUrl = objNews.Avatar
                                Me.txtLinkImage.Text = objNews.Avatar
                            Else
                                Me.imgNews.Visible = True
                                Me.imgNews.ImageUrl = "/Portals/" & PortalId & "/Product/" & objNews.Avatar
                            End If
                        End If
                        Me.txtThongtin.Text = objNews.Mota
                    Else
                        Me.lbtDeleteBottom.Visible = False
                    End If

                    '</Edit news>
                Catch ex As Exception
                    ProcessModuleLoadException(Me, ex)
                End Try
            End If
        End Sub
        Private Sub lbtCancelBottom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtCancelBottom.Click
            Try
                Response.Redirect(NavigateURL(), True)
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Private Sub lbtUpdateTop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtUpdateTop.Click
            If addnews() Then
                'clearcontroldata()
                'addnewsvideo("idtintuc")
            End If
        End Sub
        Private Function addnews() As Boolean
            Try
                If Request.Item("sanpham") <> "" Then
                    Dim sanpham As String
                    sanpham = Request.Item("sanpham")
                    'update news

                    Dim stitle As String
                    Dim stenphu As String
                    Dim iCat As Integer
                    Dim sMaSP As String
                    Dim sGia As String
                    Dim SGiaKM As String
                    Dim iSPKM As Integer
                    Dim sGiatriKM As String
                    Dim SChatlieu As String
                    Dim SXuatxu As String
                    Dim sPicture As String
                    Dim sMota As String
                    ' Insert

                    stitle = Me.txtTitle.Text
                    stenphu = Me.txtTitlePhu.Text
                    iCat = Me.ddlCategory.SelectedItem.Value
                    sMaSP = Me.txtMasp.Text
                    sGia = Me.txtGia.Text
                    SGiaKM = Me.txtGiaKM.Text
                    iSPKM = CInt(IIf(Me.chkSPKM.Checked(), 1, 0))
                    sGiatriKM = Me.txtgiatriKM.Text
                    SChatlieu = Me.txtChatlieu.Text
                    SXuatxu = Me.txtXuatxu.Text
                    If txtLinkImage.Text <> "" Then
                        sPicture = txtLinkImage.Text
                    Else
                        If Me.imgNews.ImageUrl <> "" Then
                            sPicture = Path.GetDirectoryName(imgNews.ImageUrl) & "/" & Path.GetFileName(imgNews.ImageUrl)
                            sPicture = sPicture.Replace("\Portals\" & PortalId & "\Product\", "")
                            sPicture = sPicture.Replace("\", "/")
                        Else
                            sPicture = ""
                        End If
                    End If
                    sMota = Me.txtThongtin.Text
                    DataProvider.Instance.NV_Product_Update(sanpham, iCat, stitle, stenphu, sPicture, sMaSP, sGia, SGiaKM, iSPKM, sGiatriKM, SChatlieu, SXuatxu, sMota)
                    Response.Redirect(NavigateURL(), True)
                Else
                    Dim isanpham As String
                    Dim stitle As String
                    Dim stenphu As String
                    Dim iCat As Integer
                    Dim sMaSP As String
                    Dim sGia As String
                    Dim SGiaKM As String
                    Dim iSPKM As Integer
                    Dim sGiatriKM As String
                    Dim SChatlieu As String
                    Dim SXuatxu As String
                    Dim sPicture As String
                    Dim sMota As String
                    ' Insert
                    isanpham = Date.Now.ToString("yyyMMddHHmmss")
                    stitle = Me.txtTitle.Text
                    stenphu = Me.txtTitlePhu.Text
                    iCat = Me.ddlCategory.SelectedItem.Value
                    sMaSP = Me.txtMasp.Text
                    sGia = Me.txtGia.Text
                    SGiaKM = Me.txtGiaKM.Text
                    iSPKM = CInt(IIf(Me.chkSPKM.Checked(), 1, 0))
                    sGiatriKM = Me.txtgiatriKM.Text
                    SChatlieu = Me.txtChatlieu.Text
                    SXuatxu = Me.txtXuatxu.Text
                    If txtLinkImage.Text <> "" Then
                        sPicture = txtLinkImage.Text
                    Else
                        If Me.imgNews.ImageUrl <> "" Then
                            sPicture = Path.GetDirectoryName(imgNews.ImageUrl) & "/" & Path.GetFileName(imgNews.ImageUrl)
                            sPicture = sPicture.Replace("\Portals\" & PortalId & "\Product\", "")
                            sPicture = sPicture.Replace("\", "/")
                        Else
                            sPicture = ""
                        End If
                    End If
                    sMota = Me.txtThongtin.Text
                    DataProvider.Instance.NV_Product_Insert(isanpham, iCat, stitle, stenphu, sPicture, sMaSP, sGia, SGiaKM, iSPKM, sGiatriKM, SChatlieu, SXuatxu, sMota, 0, 0, DateTime.Now, UserId, PortalId)
                    Me.lbResult.Text = "Cập nhật tin thành công"
                    Response.Redirect(NavigateURL(), True)
                End If
                Return True
            Catch ex As Exception
                Me.lbResult.Text = "Lỗi cập nhật tin"
                ProcessModuleLoadException(Me, ex)
                Return False
            End Try
        End Function
        Private Sub lbtUpdateBottom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtUpdateBottom.Click
            If addnews() Then
                clearcontroldata()
                ' addnewsvideo("idtintuc")
            End If
        End Sub
        Private Sub lbtCancelTop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtCancelTop.Click
            Try
                If Request.Item("sanpham") <> "" Then
                    'Dim tin As Integer
                    'tin = CInt(Request.Item("tin"))
                    Response.Redirect(NavigateURL(), True)
                Else
                    clearcontroldata()
                End If
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Private Sub clearcontroldata()
            Me.txtTitle.Text = ""
            Me.txtThongtin.Text = "Đang cập nhật"
            Me.imgNews.ImageUrl = ""
        End Sub
#End Region

        Private Sub lbtUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtUpload.Click
            Try
                If Page.IsPostBack Then
                    Dim PathtoWrite As String = Request.MapPath(PortalSettings.HomeDirectory) & "Product/"
                    Dim dateToUse As DateTime = DateTime.Now
                    Dim fullpath As String = String.Concat(PathtoWrite, "\", dateToUse.Year, "\", dateToUse.Month, "\", dateToUse.ToString("dd"), "\")
                    Dim shortpath As String = String.Concat(PortalSettings.HomeDirectory, "Product/", dateToUse.Year, "/", dateToUse.Month, "/", dateToUse.ToString("dd"), "/")
                    If Not System.IO.Directory.Exists(fullpath) Then
                        System.IO.Directory.CreateDirectory(fullpath)
                    End If
                    Dim strFileName As String
                    Dim strFileNamePath As String
                    Dim strExtension As String = ""
                    Dim strMessage As String = ""
                    Dim strChangeName As String
                    If Me.txtFile.PostedFile.FileName <> "" Then
                        strFileName = System.IO.Path.GetFileName(Me.txtFile.PostedFile.FileName)
                        strExtension = Path.GetExtension(strFileName)
                        strFileNamePath = System.IO.Path.GetFullPath(Me.txtFile.PostedFile.FileName)
                        strChangeName = strFileName.Replace(strFileName, DateTime.Now.Ticks & ".jpg")
                        'Me.txtFile.PostedFile.SaveAs(Request.MapPath(PortalSettings.HomeDirectory) & "News/" & strChangeName)
                        Me.txtFile.PostedFile.SaveAs(fullpath & strChangeName)
                        Me.imgNews.ImageUrl = shortpath & strChangeName
                        Me.imgNews.Visible = True
                        Me.lblMessage.Text = shortpath & strChangeName
                    End If
                End If

            Catch exc As Exception 'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try

        End Sub
        Private Sub lbtDeleteTop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtDeleteTop.Click
            Try
                If Request.Item("sanpham") <> "" Then
                    Dim sanpham As String
                    sanpham = CInt(Request.Item("sanpham"))
                    DataProvider.Instance.NV_Product_Delete(sanpham, PortalId)
                    Response.Redirect(NavigateURL(), True)
                Else
                    clearcontroldata()
                End If
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Private Sub lbtDeleteBottom_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbtDeleteBottom.Click
            Try
                If Request.Item("sanpham") <> "" Then
                    Dim sanpham As String
                    sanpham = CInt(Request.Item("sanpham"))
                    DataProvider.Instance.NV_Product_Delete(sanpham, 1)
                    Response.Redirect(NavigateURL(), True)
                Else
                    clearcontroldata()
                End If
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Private Sub BindddlCategories()
            Dim ctlNewsCategories As New NV_ProductCategoriesController
            Dim arrNewsCategories As New ArrayList
            arrNewsCategories = ctlNewsCategories.GetAll(PortalId)
            Dim arrTemp As New ArrayList
            Dim objNewsCategories As CatInfo
            Dim objNewsCategoriesTemp As CatInfo

            If arrNewsCategories.Count > 0 Then
                For Each objNewsCategories In arrNewsCategories
                    If objNewsCategories.ParentId = 0 Then
                        arrTemp.Add(objNewsCategories)
                        For Each objNewsCategoriesTemp In arrNewsCategories
                            If objNewsCategoriesTemp.ParentId = objNewsCategories.CategoryID Then
                                objNewsCategoriesTemp.CategoryName = "||---->" & objNewsCategoriesTemp.CategoryName
                                arrTemp.Add(objNewsCategoriesTemp)
                            End If
                        Next
                    End If
                Next
            End If

            Me.ddlCategory.DataSource = arrTemp
            Me.ddlCategory.DataTextField = "CategoryName"
            Me.ddlCategory.DataValueField = "CategoryId"
            Me.ddlCategory.DataBind()
        End Sub
        Public Function GetUserName(ByVal userid As Integer) As String
            If userid = -1 Then
                GetUserName = "Bạn đọc Gửi"
            Else
                Return UserController.GetUser(PortalId, userid, True).Username
            End If
        End Function
        'Nguon Tin

    End Class

End Namespace
